const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: true,
    phone: '',
    pwd: '',
    openid: ''
  },

  pointout: function () {
    wx.navigateTo({
      url: '../Aregister/Aregister',
    })
  },

  phone: function (e) {
    console.log(e);
    this.setData({
      phone: e.detail.value
    })
  },
  pwd: function (e) {
    this.setData({
      pwd: e.detail.value
    })
  },


  selected: function () {
    var that = this
    that.setData({
      state: !that.data.state
    })
  },

  login: function () {
    wx.showLoading({
      title: '登录中',
    })
    console.log("aaaa");
    var that = this
    if (!that.data.phone) {
      wx.showToast({
        title: '账号不能为空',
        icon: 'none',
        duration: 2000
      })
      return false
    }

    if (that.data.pwd.length < 6) {
      wx.showToast({
        title: '请输入6-12位密码',
        icon: 'none',
        duration: 2000
      })
      return false
    }

    let setdata = {
      username: that.data.phone,
      pwd: that.data.pwd,
      code_type: 'chuyuedu',
      openid: that.data.openid
    }
    console.log(setdata);
    wx.request({
      url: app.api + app.plogin, // 仅为示例，并非真实的接口地址
      method: 'post',
      data: setdata,
      success(e) {
        wx.hideLoading()
        console.log(e)
        if (e.data.code == 0) {
          wx.setStorage({
            key: 'puser',
            data: e.data.data,
            success: function () {
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
              })
              wx.reLaunch({
                url: '../Aindex/index'
              })
            }
          })

        } else {
          wx.showToast({
            title: e.data.msg,
            icon: 'none',
          })
        }
      },
      fail(e) {
        wx.hideLoading()
        console.log(e)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getStorage({
      key: 'puser',
      success: function (res) {
        console.log("aaa");
        wx.reLaunch({
          url: '../Aindex/index',
        })
      },
    })
    wx.getStorage({
      key: 'openid',
      success: function (openid) {
        console.log(openid)
        that.setData({
          openid: openid.data
        })
      },
      fail: function () {
        wx.login({
          success(res) {
            if (res.code) {
              //发起网络请求
              wx.request({
                url: app.api + app.popenid,
                method: 'POST',
                data: {
                  code: res.code,
                  code_type: "chuyuedu"
                },
                success(e) {
                  that.setData({
                    openid: e.data.data.openid
                  })
                }
              })
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})